import dd from 'gdt-jsapi'
import * as mock from './mock';
import globalData from '../globalData'
const { HOST, imageHost } = globalData

export const fatchData = (props) => {
  const defaultData = {
    method: "GET",
    dataType: "json",
  }
  const params = {
    ...defaultData,
    ...props,
    url: HOST + props.url
  }
  console.log('请求参数', params)
  return dd.httpRequest(params).then(res => {
    return res.data
  })
}

// 1. 根据authCode获取个人信息及组织架构信息 ok
export const getUserInfo = async () => {
  // return mock.s1
  const authCode = await dd.getAuthCode({})
  const { code, message, data } = await fatchData({
    url: '/api/ds/knowledge/platform/user/get.json',
    data: { authCode: authCode.code }
  })
  if (code !== 200) {
    dd.showToast({ typ: 'fail', content: message })
  }
  return data
  // const key = 'userInfo';
  // const { data } =  dd.getStorageSync({ key });
  // if (data) {
  //   return data
  // } else {
  //   const { code } = await dd.getAuthCode({})
  //   const { data } = await fatchData({
  //     url: '/api/ds/knowledge/platform/user/get.json',
  //     data: { authCode: code }
  //   })
  //   dd.setStorageSync({ key, data });
  //   return data
  // }
}

// 2. 根据个人员工code获取点位详细信息 ok
export const getPointLocationByCode = (data) => {
  // return mock.s2;
  return fatchData({
    url: '/api/ds/knowledge/platform/pointLocation/query.json',
    data
  })
}

// 3. 创建整改点信息 ok
export const createPoint = (data) => {
  // return mock.s3;
  return fatchData({
    headers: {
      "Content-Type": "application/json"
    },
    url: '/api/ds/knowledge/platform/remediation/create.json',
    method: 'POST',
    data
  })
}

// 4. 根据个人员工code获取整改点列表
export const getRemediationByCode = (data) => {
  // return mock.s4;
  return fatchData({
    url: '/api/ds/knowledge/platform/remediation/query.json',
    data
  })
}
// 5. 根据整改点id查询整改点详情信息
export const getRemediation = (data) => {
  // return mock.s5;
  return fatchData({
    url: '/api/ds/knowledge/platform/remediation/detail.json',
    data
  })
}

// 6. 点位长提交整改后信息
export const submitRemediationByPLocOwner = (data) => {
  // return mock.s6;
  return fatchData({
    url: '/api/ds/knowledge/platform/remediation/pLocOwner/submit.json',
    method: 'POST',
    data
  })
}

// 7. 创建办审核整改点
export const approve = (data) => {
  // return mock.s7;
  return fatchData({
    url: '/api/ds/knowledge/platform/remediation/createOffice/approve.json',
    method: 'POST',
    data
  })
}

export const getReport = async () => {
  const specialWorkingGroupRectificationRateReport = await specialWorkingGroupRectificationRate();
  const createOfficeRectificationRateReport = await createOfficeRectificationRate();
  const poLocOwnerSelfCheckRateReport = await poLocOwnerSelfCheckRate();
  const specialWorkingGroupAgingReport = await specialWorkingGroupAging();
  const poLocOwnerAgingReport = await poLocOwnerAging();
  const specialWorkingGroupDeductMarksReport = await specialWorkingGroupDeductMarks();
  const poLocOwnerdeDuctMarksReport = await poLocOwnerdeDuctMarks();
  return {
    specialWorkingGroupRectificationRateReport: specialWorkingGroupRectificationRateReport.data,
    createOfficeRectificationRateReport: createOfficeRectificationRateReport.data,
    poLocOwnerSelfCheckRateReport: poLocOwnerSelfCheckRateReport.data,
    specialWorkingGroupAgingReport: specialWorkingGroupAgingReport.data,
    poLocOwnerAgingReport: poLocOwnerAgingReport.data,
    specialWorkingGroupDeductMarksReport: specialWorkingGroupDeductMarksReport.data,
    poLocOwnerdeDuctMarksReport: poLocOwnerdeDuctMarksReport.data,
  }
}

// 8. 查询专线组督查整改率
export const specialWorkingGroupRectificationRate = (data) => {
  return fatchData({
    url: '/api/ds/knowledge/platform/report/specialWorkingGroup/rectificationRate/query.json',
    data
  })
}

// 9. 查询创建办督查整改率
export const createOfficeRectificationRate = (data) => {
  return fatchData({
    url: '/api/ds/knowledge/platform/report/createOffice/rectificationRate/query.json',
    data
  })
}

// // 10. 查询点位长自查率
export const poLocOwnerSelfCheckRate = (data) => {
  return fatchData({
    url: '/api/ds/knowledge/platform/report/poLocOwner/selfCheckRate/query.json',
    data
  })
}

// 11. 查询完成的整改点各专线组平均时效排名
export const specialWorkingGroupAging = (data) => {
  return fatchData({
    url: '/api/ds/knowledge/platform/report/specialWorkingGroup/aging/query.json',
    data
  })
}

// 12. 查询完成的整改点各点位长平均时效排名
export const poLocOwnerAging = (data) => {
  return fatchData({
    url: '/api/ds/knowledge/platform/report/poLocOwner/aging/query.json',
    data
  })
}

// 13. 查询未完成的整改点各专线组累计扣分排名
export const specialWorkingGroupDeductMarks = (data) => {
  return fatchData({
    url: '/api/ds/knowledge/platform/report/specialWorkingGroup/deductMarks/query.json',
    data
  })
}

// 14. 查询未完成的整改点各点位长累计扣分排名
export const poLocOwnerdeDuctMarks = (data) => {
  return fatchData({
    url: '/api/ds/knowledge/platform/report/poLocOwner/deductMarks/query.json',
    data
  })
}

// 13. 文件上传 ok
export const uploadFile = (data) => {
  // console.log(data)
  return new Promise((resolve) => {
    my.uploadFile({
      url: `${HOST}/api/ds/knowledge/platform/file/upload.json`,
      fileType: 'image',
      fileName: 'file',
      filePath: data.filePath,
      success: res => {
        const { data } = JSON.parse(res.data)
        console.log('data', data)
        resolve(data)
        my.alert({ title: '上传成功' });
      },
      fail: function (res) {
        my.alert({ title: '上传失败' });
      },
    });
  })
}
